package org.windowkit.toolkit.widget.table.event;

import java.util.EventObject;

import org.ddevil.data.util.DataUtils;


/**
 * This event object is used with TableEditedListener to describe a table cell
 * which was edited and allows the developer to easily determine what changes
 * were made.
 * @author Eric Lundin
 */
public class TableEditedEvent extends EventObject {

	private static final long serialVersionUID = 4507607689228738039L;

	/** The row which was edited. */
	private int row;

	/** The column which was edited. */
	private int column;

	/** The value the cell contained before being edited. */
	private Object oldValue;

	/** The value the cell now contains. */
	private Object newValue;

	/**
	 * @param source The object this event occurred on.
	 * @param editRow The row that was edited.
	 * @param editColumn The column which was edited.
	 */
	public TableEditedEvent(final Object source, final int editRow, final int editColumn) {
		super(source);
		setEditedCell(editRow, editColumn);
	}

	/**
	 * Set the value this cell used to contain.
	 *
	 * @param value
	 *            The old value of this cell.
	 */
	public void setOldValue(final Object value) {
		oldValue = value;
	}

	/**
	 * Set the value this cell currently contains.
	 *
	 * @param value
	 *            The value this cell now contains.
	 */
	public void setNewValue(final Object value) {
		newValue = value;
	}

	/**
	 * Set the cell which this edit took place on.
	 *
	 * @param editRow
	 *            The row which was edited.
	 * @param editColumn
	 *            The column which was edited.
	 */
	public void setEditedCell(final int editRow, final int editColumn) {
		row = editRow;
		column = editColumn;
	}

	/**
	 * Get the row index which was edited.
	 *
	 * @return The index of the row which was edited.
	 */
	public int getEditRow() {
		return row;
	}

	/**
	 * Get the column index which was edited.
	 *
	 * @return The index of the column which was edited.
	 */
	public int getEditColumn() {
		return column;
	}

	/**
	 * Get the new value for this cell.
	 *
	 * @return The value this cell now contains.
	 */
	public Object getNewValue() {
		return newValue;
	}

	/**
	 * Get the old value for this cell.
	 *
	 * @return The value this cell used to contain.
	 */
	public Object getOldValue() {
		return oldValue;
	}

	/**
	 * Set the source this event occurred on. To be used by delegates.
	 *
	 * @param newSource
	 *            The source object which this event occured on.
	 */
	public void setSource(final Object newSource) {
		super.source = newSource;
	}

    /**
     * Did the edit result in the data being changed?
     * Just a convenience method that does a safe equals on
     * the two values.
     *
     * @return
     */
    public boolean hasChanged(){
        return !DataUtils.safeEquals(oldValue, newValue);
    }

    /**
     * Set the column where the edit occurred.
     * @param column
     */
    public void setColumn(final int column) {
        this.column = column;
    }

    /**
     * Set the row where the edit occurred.
     *
     * @param row
     */
    public void setRow(final int row) {
        this.row = row;
    }

    @Override
    public String toString() {
        String ret = "Row: " + row + ", Col: " + column + ", Old: "
        + (oldValue != null ? oldValue.toString() : oldValue)
        + ", New: "
        + (newValue != null ? newValue.toString() : newValue);
        return ret;
    }
}
